import { _decorator, Component, Node } from "cc";
import { getGlobal, MyGlobal } from "../Global";
const { ccclass, property } = _decorator;

@ccclass("User")
export class User extends Component {
  constructor() {
    super();
    this.init();
  }
  statics = {
    account: null,
    userId: null,
    userName: null,
    lv: 0,
    exp: 0,
    coins: 0,
    gems: 0,
    sign: 0,
    ip: "",
    sex: 0,
    roomData: null,
    oldRoomId: null,
  };
  g: MyGlobal;
  onLoad() {
    console.log("user onLoad");
    this.init();
  }
  start() {
    console.log("user start");
  }
  init() {
    let user = cc.sys.localStorage.getItem("user");
    if (user) {
      try {
        user = JSON.parse(user);
        if(user && user.account){
          this.statics = user;
        }
      } catch (err) {}
    }
  }
  travelerAuth() {
    console.log("start auth");
    let account = this.statics.account;
    if (account == null) {
      account = cc.sys.localStorage.getItem("account");
    }
    if (account == null) {
      account = Date.now();
      cc.sys.localStorage.setItem("account", account);
    }
    let g = getGlobal();
    if (!this.g) {
      this.g = g;
    }
    if(!this.g.SI) {
      return ;
    }
    this.g.http.setURL("http://" + this.g.SI.account);
    console.log(this.g.SI.account,"?this.g.SI.account")
    g.http.sendRequest("/guest", { account }, (ret) => {
      this.onAuth(ret);
    });
  }
  onAuth(ret) {
    if (ret.errcode !== 0) {
      console.log(ret.errmsg);
    } else {
      this.statics.account = ret.account;
      this.statics.sign = ret.sign;
      // this.g.http.url = "http://" + this.g.SI.hall;
      this.g.http.setURL("http://" + this.g.SI.hall);
      this.login();
    }
  }
  login() {
    // this.g.http.setURL("http://" + this.g.SI.account);

    let g = getGlobal();
    if (!this.g) {
      this.g = g;
    }
    this.g.http.sendRequest(
      "/login",
      {
        account: this.statics.account,
        sign: this.statics.sign,
      },
      (ret) => {
        if (ret.errcode === 0) {
          console.log(ret, "?ret", this.statics);
          if (!ret.userid && !this.statics.userId) {
            cc.director.loadScene("createrole");
          } else {
            this.statics.account = ret.account;
            this.statics.userId = ret.userid;
            this.statics.userName = ret.name;
            this.statics.lv = ret.lv;
            this.statics.exp = ret.exp;
            this.statics.coins = ret.coins;
            this.statics.gems = ret.gems;
            this.statics.roomData = ret.roomid;
            this.statics.sex = ret.sex;
            this.statics.ip = ret.ip;
            cc.sys.localStorage.setItem("user", JSON.stringify(this.statics));
            cc.director.loadScene("hall");
          }
        }
      }
    );
  }
  create(name) {
    let data = {
      account: this.statics.account,
      sign: this.statics.sign,
      name,
    };
    this.g.http.sendRequest("/create_user", data, (ret) => {
      if (ret.errcode !== 0) {
      } else {
        this.login();
      }
    });
  }
  timer;
  enterRoom(roomId, cb?: (...args) => any) {
    let data = {
      account: this.statics.account,
      sign: this.statics.sign,
      roomid: roomId,
    };

    let g = getGlobal();
    if (!this.g) {
      this.g = g;
    }
    g.http.sendRequest("/enter_private_room", data, (ret) => {
      if (ret.errcode !== 0) {
        if (ret.errcode == -1) {
          this.timer = setTimeout(() => {
            this.enterRoom(roomId, cb);
          }, 5000);
        } else {
          if (cb) {
            cb(ret);
          }
        }
      } else {
        if (cb) {
          cb(ret);
        }
        // this.g.gameNet.connectGameServer(ret);
      }
    });
  }
  clearEnter(){
    clearTimeout(this.timer);
  }

  update(deltaTime: number) {}
}
